package com.li.core.controller.es;

import com.li.configserve.domain.gloabreturn.ResponseInfo;
import com.li.configserve.utils.es.EsUtils;
import com.li.configserve.domain.rabbitqueue.QueueConst;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description：
 * @Author：LiDong
 * @Create：2020/8/25
 * @Version：1.0.0
 */
@RequestMapping("/es")
@RestController
public class EsController {

    private static final Logger logger = LoggerFactory.getLogger(EsController.class);

    @Autowired
    private RestHighLevelClient client;


    @RequestMapping("/createindex")
    public ResponseInfo createIndex() {
        try {
            CreateIndexRequest createIndexRequest = new CreateIndexRequest("user");
            CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
            logger.info(String.valueOf(createIndexResponse));
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/test1")
    public void test1() {
        boolean existsIndex = EsUtils.isExistsIndex(client, QueueConst.GLOBAL_LOG_QUEUE);
        logger.info(String.valueOf(existsIndex));
        EsUtils.deleteIndex(client, QueueConst.GLOBAL_LOG_QUEUE);
        // SysLogs sysLogs=new SysLogs();
        // sysLogs.setId(UUIDUtils.randomUuid());
        // sysLogs.setUrl("test");
        // sysLogs.setRecorderTime(new Date());
        // EsUtils.addDocument(client,QueueConstant.LOG_QUEUE,sysLogs);
        // boolean existsDocument = EsUtils.isExistsDocument(client, QueueConstant.LOG_QUEUE, "3");
        // logger.info(String.valueOf(existsDocument));
    }

    @RequestMapping("/deleteindex")
    public void test2() {
        EsUtils.deleteIndex(client, QueueConst.GLOBAL_LOG_QUEUE);
    }


}
